<template>
  <div class="company_create">
    <setHeader :title="headerTitle" :back-fn="goback"></setHeader>
    <div class="pagecontent">
        <!-- <div class="default_input_box main-company-input" style="margin-top: 0.2rem;">
            <input type="text" placeholder="请输入绑定的企业" v-model="companyName" :attr="{maxlength:30}" v-if="nameFlag" style="width: 100% !important;" @input="matchCompany($event)">
        </div> -->
        <!-- 检索 -->
        <!-- <div class="creatematch">
            <ul @click="selectCompany($event)">
                <li v-for="(item, i) in companyList" :c_id="item.id" :key="i">{{item.name}}</li>
            </ul>
        </div> -->
        
        <!-- 请输入绑定的企业 -->
        <Autocomplete :placeholder="placeholder" @dater='companyClick' :companyShow="true"></Autocomplete>
        <div class="btn_big_red" @click="checkCompany" v-if="nameFlag" style="margin-top: 0.9rem;">
            <button>下一步</button>
        </div>
        
    </div>
    <!-- <mt-field label="" placeholder="请输入企业名称" v-model="companyName" :attr="{maxlength:30}" class="company_name" v-if="nameFlag"></mt-field> 
    <mt-button type="primary"  class="el-long-button el-long-create-button y-btn-red" size="normal"  @click.native="checkCompany" v-if="nameFlag">下一步</mt-button>-->
  </div>
</template>

<script>
import setHeader from '../public/setHeader.vue';
import Autocomplete from '@/components/companyAutocomplete';
  import { MessageBox } from 'mint-ui';
  export default {
 
    data() {
      return {
        headerTitle:'企业申请',
        placeholder: '请输入绑定的企业',
        companyName:'',
        nameFlag: true,
        userInfo:'',
        log_img: '',
        isShow: true,
        companyList: [],
      };
    },
    components: {
      setHeader,
      Autocomplete
    },
    created() {
      let that = this;
      that.userInfo = window.localStorage.getItem('userInfo');
      that.userInfo = JSON.parse(that.userInfo);
      // that.matchCompany();
    },
    computed: {
        //过滤方法
        items: function() {
            var _search = this.companyName;
            if (_search) {
                //不区分大小写处理
                var reg = new RegExp(_search, 'ig')
                //es6 filter过滤匹配，有则返回当前，无则返回所有
                return this.companyList.filter(function(e) {
                    //匹配所有字段
                    return Object.keys(e).some(function(key) {
                        return e[key].match(reg);
                    })
                    //匹配某个字段
                    //  return e.name.match(reg);
                })
            };
            return this.companyList;
        }
    },  
    methods: {
      // 企业名字
      companyClick(company_name) {
        this.companyName = company_name;
      },
      //创建企业提示企业
      checkCompany(){
        let that = this;
        var companyName = that.companyName;
        var userId = that.userInfo.uid;
        // console.log('企业名称', userId);
        if(companyName.length<4){
          MessageBox('提示', '请输入企业全称')
          return false;
        }
        if(!companyName){
          MessageBox('提示', '请输入企业名称')
          return false;
        }else{

          that.$fetch(that.$path + '/api.php/company/getCompanyExist', {companyName, userId}).then((response) => {
            console.log('头像111',response);
            if(response.code == 2001){
              var adminName = response.data.adminName;
              var tel = response.data.tel;
              var cp_id = response.data.cp_id;
              response.data.log_img = this.log_img;
              // console.log('头像22',log_img)

              that.$router.push({path: '/inCompany', query: {companyName:companyName,cp_id:cp_id, adminName:adminName, tel:tel, log_img:this.log_img}}); 
            }else if(response.code == 2002){
              that.$router.push({path: '/applyCompanyAdmin', query: {companyName:companyName, adminName:adminName, tel:tel}});
            }else if(response.code == 2003){
              that.$router.push({path: '/createNewCompany', query: {companyName:companyName}});
            }else{
              MessageBox('提示', response.msg)
              return false;
            }
          })
        }
      },
      goback() {
        this.$router.push({ path: '/userCenter' });
      },
      /**
       * 模糊查询企业
       */
      matchCompany (e){
        // 隐藏start
          var that = this;
          console.log('===>',e)
          var keyWords = e.target.value;
          console.log(keyWords)
          if (keyWords == '') {
            that.companyList = [];
            return ;
          }
        // 隐藏end
          var data = {
              access_token: this.access_token,
              keyWords: keyWords
          }
          this.$post(this.$path + '/index.php/api/matchCompany', data).then((response)=>{
              console.log('所有企业',response);
                if(response.code == 200){
                  that.companyList = response.data;
                }else{
                  that.companyList = [];
                }
            })
      },
      /**
       * 筛选选择哪个企业
       */
      selectCompany(e){
          var that = this;
          var data = {
              access_token: this.access_token,
              id: e.target.attributes.c_id.value
          };
          this.$post(this.$path + '/index.php/api/companyDetails', data).then((response)=>{
              console.log(response);
                if(response.code == 200){
                  that.companyDetails = response.data;
                  that.companyName = response.data.name;
                  that.trade = response.data.trade;
                  // $('#trade')[0].disabled = true;
                }else{

                }
            })
            this.companyList = [];
      },
    }
  }

</script>
<style >
.mintui{
    font-size: 0.36rem;
  }
.mint-cell-wrapper{
   padding-left:   0.3rem ;
   height: 0.94rem  ;
 }
 .mint-field-core{
   font-size: 0.28rem ;
   color: #333 ;
 }

 /* 请输入企业名称 */
 .main-company-input {
   margin-bottom: 1.4rem;
 }
 /* 头部颜色 */
 .company_create .header_array_text_white {
   background-color: #fff;
   color: #000;
 }
 .company_create .header_array_text_white .title_white {
   color: #000;
 }

/* 检索 */
 .creatematch{
    position: absolute;
    z-index: 1;
    width: 100%;
    background: #fff;
    overflow: hidden;
    box-shadow: 1px 1px 3px #ededed;
    -webkit-box-shadow: 1px 1px 3px #ededed;
    -moz-box-shadow: 1px 1px 3px #ededed;
    -o-box-shadow: 1px 1px 3px #ededed;
    top: 1.21rem;
    text-align: left;
    line-height: 0.56rem;
    font-size: 0.38rem;
    color: #666;
    padding-left: 0.2rem;
}

</style>
